/*
* 库存查询
*/
public with sharing class ResposityQueryController {
	 public String resJson {get; private set;}
   public String company {get; private set;}

   public String searchJson {get; set;}
    public ResposityQueryController() {
       

    }

    public void init(){

        User u = [select Id,SubCompany__c from User where Id=:UserInfo.getUserId()];
        List<Object> companys = new List<Object>();
        Map<String,String> mM = ImportObject.getPicklistValues('Opportunity', 'Corpcode__c', true);
       for(String  m : mM.keyset()){
          Map<String,String> d = new Map<String,String>();
          d.put('id', m);
          d.put('text', mM.get(m));
          companys.add(d);
          if(u.SubCompany__c == mM.get(m)){
            company = m;
          }
       }
      resJson = EncodingUtil.urlEncode(JSON.serialize(companys), 'UTF-8') ;


    }
    public void query(){

     	String url = Label.queryResposityURL;

      Map<String,Object > searchMap = (Map<String,Object > )JSON.deserializeUntyped(EncodingUtil.urlDecode(searchJson, 'UTF-8'));
      String sql = 'select a.bodyname ,a.invname,a.invcode, a.storname ,a.invdm,a.invspec,a.invtype,a.invpinpai ,a.vbatchcode,a.pzwh   ,  a.dproducedate ,  a.dvalidate ,  a.nonhandnum   ,  a.nfreezenum   from  v_xclinfo_esb a where a.storname like \'%配件库%\'  ';

      if(String.isNotBlank((String)searchMap.get('corpcode'))) sql += ' and a.corpcode = \''+searchMap.get('corpcode')+'\'';
      if(String.isNotBlank((String)searchMap.get('invname'))) sql += ' and a.invname like \'%'+searchMap.get('invname')+'%\'';
      if(String.isNotBlank((String)searchMap.get('invcode'))) sql += ' and a.invcode like \'%'+searchMap.get('invcode')+'%\'';

      if(String.isNotBlank((String)searchMap.get('storname'))) sql += ' and a.storname like \'%'+searchMap.get('storname')+'%\'';

      if(String.isNotBlank((String)searchMap.get('invspec'))) sql += ' and a.invspec like \'%'+searchMap.get('invspec')+'%\'';
      if(String.isNotBlank((String)searchMap.get('invdm'))) sql += ' and a.invdm like \'%'+searchMap.get('invdm')+'%\'';

      if(String.isNotBlank((String)searchMap.get('invpinpai'))) sql += ' and a.invpinpai like \'%'+searchMap.get('invpinpai')+'%\'';
      if(String.isNotBlank((String)searchMap.get('vbatchcode'))) sql += ' and a.vbatchcode like \'%'+searchMap.get('vbatchcode')+'%\'';

      if(String.isNotBlank((String)searchMap.get('startDate'))) sql += ' and a.dproducedate >= \''+searchMap.get('startDate')+'\'';
      if(String.isNotBlank((String)searchMap.get('endDate'))) sql += ' and a.dproducedate <= \''+searchMap.get('endDate')+'\'';


     	 sql = '{"data":" '+sql +' "}';
     	 //请求

      HttpRequest req = new HttpRequest();
      req.setBody(sql);
      System.debug(sql);
      req.setMethod('POST');
      req.setHeader('Content-Type','text/plain');
      req.setEndpoint(url);
      HttpResponse res = Utils.postRequest(req, 2);
      ///返回结果处理
        if (res.getStatusCode() == 200) {
      		resJson = EncodingUtil.urlEncode(res.getBody(), 'UTF-8') ;
	    }else{
	    	 System.debug(res.toString());
	    	resJson = EncodingUtil.urlEncode('{"result":"","code":"1","msg":"接口错误,查询失败!"}','UTF-8');
	    }

    }
    public void setNil(){
    	resJson = '';
    }


}